<template>
  <div class="main-app">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="form-inline">
        <el-form-item label="废物名称:">
          <el-input v-model="formInline.name" clearable />
        </el-form-item>
        <el-form-item label="废物代码:">
          <el-input v-model="formInline.wasteCode" clearable />
        </el-form-item>
        <el-form-item label="危险特性:">
          <el-select v-model="formInline.wasteCharacteristics" placeholder="请选择危险特性" clearable>
            <el-option v-for="item in wasteCharacteristicsOptions" :key="item.value" :label="item.label"
                       :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态:">
          <el-select v-model="formInline.status" placeholder="请选择状态" clearable>
            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh-right" @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="show-result">

      <!-- 加载动态菜单 -->
      <el-form :inline="true" class="form-inline">
        <el-form-item v-for="item in toolbtn" :key="item.id">
          <el-button type="primary" :icon="item.meta.icon" @click="showViews(item.name)">
            {{ item.meta.title }}
          </el-button>
        </el-form-item>
      </el-form>

      <!-- <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" @click="add">
            新增
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-edit" :disabled="disabled" @click="edit">
            编辑
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-view" :disabled="disabled" @click="view">
            查看详情
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-document-checked" :disabled="deleteDisabled" @click="enable('0')">
            启用
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-document-delete" :disabled="deleteDisabled" @click="enable('1')">
            禁用
          </el-button>
        </el-form-item>
      </el-form> -->

      <div class="result">
        <el-table ref="multipleTable" v-loading="loading" border :data="tableData" tooltip-effect="dark"
                  style="width: 100%" align="center" @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="name" label="废物名称" />
          <el-table-column prop="categoryContent" label="废物类别" />
          <el-table-column prop="codeId" label="废物代码" />
          <!-- <el-table-column label="危险特性" width="280">
            <template slot-scope="scope">
              <el-tag size="medium">{{
                scope.row.characteristicContent
              }}</el-tag>
            </template>
          </el-table-column> -->
          <el-table-column label="危险特性" prop="characteristicContent" width="280">
            <template slot-scope="scope">
              <el-tag style="width: 100%; text-align: center;" effect="dark" :class="'bg-'+scope.row.characteristicContentColor">{{
                scope.row.characteristicContent
              }}</el-tag>
            </template>
          </el-table-column>
          <!-- <el-table-column label="废物形态">
            <template slot-scope="scope">
              <el-tag size="medium">{{
                scope.row.physicalProperties | showPhysicalProperties
              }}</el-tag>
            </template>
          </el-table-column> -->
          <el-table-column label="废物形态" prop="physicalProperties" width="100">
            <template slot-scope="scope">
              <el-tag style="width: 100%; text-align: center;" effect="dark" :class="'bg-'+scope.row.physicalPropertiesColor">{{
                scope.row.physicalPropertiesName
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="hazardousSubstancesName" label="危害成分" />
          <!-- <el-table-column label="容器类型">
            <template slot-scope="scope">
              <el-tag size="medium">{{
                scope.row.containerCategory | showContainerCategory
              }}</el-tag>
            </template>
          </el-table-column> -->
          <el-table-column label="容器类型" prop="containerCategory" width="100">
            <template slot-scope="scope">
              <el-tag style="width: 100%; text-align: center;" effect="dark" :class="'bg-'+scope.row.containerCategoryColor">{{
                scope.row.containerCategoryName
              }}</el-tag>
            </template>
          </el-table-column>
          <!-- <el-table-column label="状态">
            <template slot-scope="scope">
              <el-tag size="medium">{{ scope.row.status | showStatus }}</el-tag>
            </template>
          </el-table-column> -->
          <el-table-column label="状态" prop="status" width="80">
            <template slot-scope="scope">
              <el-tag style="width: 100%; text-align: center;" effect="dark" :class="'bg-'+scope.row.statusColor">{{ scope.row.statusName }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="mainComponents" label="主要成分" />
        </el-table>
        <div class="page">
          <el-pagination background :current-page.sync="currentPage" layout="total,prev, pager, next, sizes"
                         :total="total" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" @current-change="handleCurrentChange"
                         @size-change="handleSizeChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  pageMixin
} from "../../../mixins/index";
import {
  getWasteInformationList,
  enableWasteInformation,
} from "../../../api/system";
import {
  EquipmentStatus,
  WasteCharacteristics,
  PhysicalProperties,
  ContainerType,
} from "../../../utils/resource";
export default {
  filters: {
    showContainerCategory(value) {
      var obj = ContainerType.find((p) => p.value === value);
      if (obj) {
        return obj.label;
      } else {
        return "无数据"
      }
    },
    showPhysicalProperties(value) {
      var obj = PhysicalProperties.find((p) => p.value === value);
      if (obj) {
        return obj.label;
      } else {
        return "无数据"
      }
    },
    showCharacteristicContent(value) {
      var obj = WasteCharacteristics.find((p) => p.value === value);
      if (obj) {
        return obj.label;
      } else {
        return "无数据"
      }
    },
    showStatus(value) {
      var obj = EquipmentStatus.find((p) => p.value === value);
      if (obj) {
        return obj.label;
      } else {
        return "无数据"
      }
    },
  },
  mixins: [pageMixin],
  data() {
    return {
      formInline: {
        name: "",
        wasteCode: "",
        wasteCharacteristics: "",
        status: "",
      },
      wasteCharacteristicsOptions: WasteCharacteristics,
      statusOptions: EquipmentStatus,
      toolbtn: [] // 列表顶部按钮
    };
  },
  async created() {
    await this.init();
  },
  methods: {
    // 列表顶部功能按钮点击事件
    showViews(action) {
      // console.log(action);
      switch (action) {
        case '新增':
          this.add();
          break;
        case '编辑':
          if (this.ids.length != 1) {
            this.$message.error('此功能仅限操作1条数据！');
            return;
          }
          this.edit();
          break;
        case '查看详情':
          if (this.ids.length != 1) {
            this.$message.error('此功能仅限操作1条数据！');
            return;
          }
          this.view()
          break;
        case '启用':
          if (this.ids.length != 1) {
            this.$message.error('此功能仅限操作1条数据！');
            return;
          }
          this.enable('0');
          break;
        case '禁用':
          if (this.ids.length != 1) {
            this.$message.error('此功能仅限操作1条数据！');
            return;
          }
          this.enable('1');
          break;
      }
    },
    async enable(value) {
      let confimText = "";
      if (value === "0") {
        confimText = "启用";
      } else {
        confimText = "禁用";
      }
      this.$confirm(
        `此操作将${confimText}这些废物基本信息, 是否继续?`,
        "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(async() => {
        const ids = this.ids.map((p) => p.id);
        await enableWasteInformation(ids, value);
        await this.init();
        this.$message({
          type: "success",
          message: `${confimText}成功!`,
        });
      });
    },
    view() {
      this.$router.push(`/system/baseinformation/view/${this.id}`);
    },
    edit() {
      this.$router.push(`/system/baseinformation/edit/${this.id}`);
    },
    add() {
      this.$router.push("/system/baseinformation/create");
    },
    async reset() {
      this.formInline = {
        name: "",
        wasteCode: "",
        wasteCharacteristics: "",
        status: "",
      };
      await this.init();
    },
    async init() {
      this.tableData = [];
      this.loading = true;
      const {
        data,
        count
      } = await getWasteInformationList({
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        Name: this.formInline.name,
        wasteCode: this.formInline.wasteCode,
        characteristicContent: this.formInline.wasteCharacteristics,
        status: this.formInline.status.toString(),
      });

      this.tableData = data;
      this.tableData.forEach(item => {
        item.pos = (item.wareHouseName === null ? "" : item.wareHouseName + "-") + (item
          .wareHouseLocationName === null ? "" : item.wareHouseLocationName)

        var obj = PhysicalProperties.find((p) => p.value === `${item.physicalProperties}`);
        if (obj) {
          item.physicalPropertiesName = obj.label;
          item.physicalPropertiesColor = obj.color;
        } else {
          item.physicalPropertiesName = "无数据";
          item.physicalPropertiesColor = "red";
        }
        var obj1 = EquipmentStatus.find((p) => p.value === `${item.status}`);
        if (obj1) {
          item.statusName = obj1.label;
          item.statusColor = obj1.color;
        } else {
          item.statusName = "无数据";
          item.statusColor = "red";
        }
        var obj2 = WasteCharacteristics.find((p) => p.label === `${item.characteristicContent}`);
        if (obj2) {
          item.characteristicContentColor = obj2.color;
        } else {
          item.characteristicContentColor = "red";
        }

        var obj3 = ContainerType.find((p) => p.value === `${item.containerCategory}`);
        if (obj3) {
          item.containerCategoryName = obj3.label;
          item.containerCategoryColor = obj3.color;
        } else {
          item.containerCategoryName = "无数据";
          item.containerCategoryColor = "red";
        }
      });
      this.total = count;
      this.loading = false;

      // 初始化按钮数据
      this.toolbtn = JSON.parse(localStorage.getItem('废物基本信息管理'));
      // console.log(this.toolbtn);
    },
  },
};
</script>

<style lang="scss" scoped>
  .result {
    margin-top: 10px;
  }

  .show-result {
    margin-top: 10px;
  }

  .abow_dialog {
    display: flex;
    justify-content: center;
    align-items: Center;
    overflow: hidden;

    .el-dialog {
      margin: 0 auto !important;
      height: 90%;
      overflow: hidden;

      .el-dialog__body {
        position: absolute;
        left: 0;
        top: 54px;
        bottom: 0;
        right: 0;
        padding: 0;
        z-index: 1;
        overflow: hidden;
        overflow-y: auto;
      }
    }
  }
</style>
